<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<div class="row" style="margin:0;padding:0;"> 
		<div class="col-xs-2" style="margin:0px auto;padding:0px;">
					 <div class="widget-box"  style="margin:0 auto;">
												<div class="widget-header widget-header-flat">
													<b>备课计划树</b>
												</div> 
												<div class="widget-body"> 
													<div style="padding:4px;">
														<div style="margin-top:4px;">
															<button id="id-btn-dialogk" class="btn btn-sm btn-primary">增加</button>
															<button id="id-btn-dialogf" class="btn btn-sm btn-primary">引用</button> 
														</div>
														<div  style="margin-top:4px;">
															<button id="id-btn-dialogc" class="btn btn-sm btn-primary">编辑</button>
															<button id="id-btn-dialogd" class="btn btn-sm btn-danger">删除</button> 
														</div> 
													 </div>
													 
												 	<ul id="treeDemo3" class="ztree"></ul>
												</div>
					 </div>
			</div>
			<div class="col-xs-10"  id="class_mainContent" style="margin:0px auto;padding:0px;padding-left:4px;">
					<!-- 关于环节 -->
					<div class="widget-box" style="margin:0 auto;">
						<div class="widget-header widget-header-flat">
							<b>关于[<font id="table_title" style="color:red;"></font>]环节列表</b>
						</div>
						
						<div class="widget-body" style="min-height:300px;">  
						 	<div class="" style="padding: 5px;">  
								<div style="float:right;">
									<input type="text" id="serch_text" style="width:250px" placeholder="请输入名称">
								</div>
							</div>
							<div style="padding:5px;">
								 <table class="table table-striped table-hover table-bordered">
								  <thead> 
										<tr id="sys_learnPlant_table_title">
											  <th  width="50">编号</th>
											  <th  width="100" fname="name">环节名称</th>
											  <th  width="300" fname="pName">环节目录</th>
											   <!-- <th  width="100" fname="content">主要内容</th> -->
											  <!-- <th  width="100" fname="learntime">时间</th> -->
											  <th  width="100" fname="adress">地点</th>
											  <th  width="40" fname="keshi">课时</th> 
											  <th  width="150" fname="string3">备课人</th> 
											  <th  width="100" fname="string2">作业</th> 
											 
											  <th  width="100" fname="showTime">时间</th>
										 </tr>
								 	 </thead>
									  <tbody id="sys_learnPlant_table">
										 
									  </tbody>
									</table> 
									<ul class="pagination" id="learnPlant_pagination">
									  
									</ul>  
							</div>		 
						</div>
						
					</div> 
					<!-- 关于环节 --> 
					<!-- 关于步骤 --> 
					<div class="widget-box" style="margin:0 auto;">
						<div class="widget-header widget-header-flat">
							<b>教学步骤</b>
						</div> 
						<div class="widget-body" style="min-height:100px;">   
							<div style="padding:5px;">
								 <table class="table table-striped table-hover table-bordered">
								  	<tr>
							 			<td>步骤名称</td>
							 			<td>时长</td>
							 			<td>内容</td>
							 			<td>附件</td>
							 			<td>标签</td>
							 			<td>场地</td>
							 			<td>教学器材</td> 
							 		</tr> 
							 		<tbody id="learnPlant_cd1">
							 		
							 		</tbody>
								</table> 	 
							</div>		 
						</div>
					</div> 
					<!-- 关于步骤 --> 
			</div>  
</div> 
<script>
(function(){
	var setting = {	
			view: {
				dblClickExpand: false,
				showLine: true,
				selectedMulti: false
			},callback: {
				onClick: zTreeOnClick
			},
			data: { 
			simpleData: {
				enable:true,
				idKey: "lpid",
				pIdKey: "plpid", 
				rootPId: "0"
			}
		}};
		//点击事件
		function zTreeOnClick(event, treeId, treeNode) { 	
			 $("#table_title").text(treeNode.name);
			 loadlearnPlant(null,{pltype:treeNode.pltype,lpid:treeNode.lpid});
		};
	   function loadTree(){ 
		   var url ="${pageContext.request.contextPath}/edu/sys_LearnPlantAction.do?method=getSys_LearnPlant";
			$.ajax({
				   type: "POST",
				   url: url,				
				   dataType:"json",
				   success: function(msg){  
					   $.fn.zTree.init($("#treeDemo3"), setting, msg[0].list);
				   }
			}); 
	   }
	function addClass(){
		//增加班
		$("#id-btn-dialogk").click(function(){ 
				
				if("${sessionScope.sysUser.userType}"=="6" || "${sessionScope.sysUser.userType}"=="4"){
					alert("你无权增加备课树!");
					return;
				}
		
				var treeObj = $.fn.zTree.getZTreeObj("treeDemo3");
				var nodes = treeObj.getSelectedNodes(); 
				var node={};
				if(nodes!=null && nodes.length>0){
					node=nodes[0];
				} 
				if(node.pltype<3 || node.pltype==null){
					if("${sessionScope.sysUser.userType}"!="5"){
					alert("只有专家才有权对课程进行规划!");
					return;
				} 
				} 
				if(node.pltype>=4){
					alert("不支持在环节下在增加具体的信息!");
					return;
				}
			 	$("#user_include").html("");
			 	$("#user_include").load("${pageContext.request.contextPath}/learnPlantAdd.jsp?r="+Math.random(),node,function(){
			 		 
			 		var dialog = $("#dialog-message").removeClass("hide").dialog({
						modal: true,
						title: "增加学期计划",
						width:1000,
						title_html: false,
						close:function(){
							$(this).dialog('destroy'); 
							$("#user_include").html("");
							//loadTree();
						},
						buttons: [  
							{
								text: "提交",
								"class" : "btn btn-primary btn-xs",
								click: function() {
									window.learnPlantAdd.addlearnPlant(this);
									 
								} 
							}
						]
					}); 
				}); 
		}); 
		//删除
		$("#id-btn-dialogd").click(function(){
			
			var r=confirm("删除节点，删除节点会删除些节点下的所有信息，你确认要删除这个节点吗?");
			  if (r==true){
				    var treeObj = $.fn.zTree.getZTreeObj("treeDemo3");
					var nodes = treeObj.getSelectedNodes();
					if(nodes!=null && nodes.length>0){
						$.ajax({
							   type: "POST",
							   async:false,
							   url: "${pageContext.request.contextPath}/edu/sys_LearnPlantAction.do?method=deletelearnPlant&r="+Math.random(),
							   data: {lpid:nodes[0].lpid,pName:nodes[0].pName,pltype:nodes[0].pltype},
							   dataType:"json",
							   success: function(msg){ 
							      if(msg.code==1){
							    	  alert(msg.message);
							    	  
							    	  loadTree();
							      }else{
							    	  alert(msg.message);
							      }
							   }
						});
					}else{
						alert("未选中节点");
					}
					
			   }
			  
		});
		//修改
		$("#id-btn-dialogc").click(function(){
				if("${sessionScope.sysUser.userType}"=="6" || "${sessionScope.sysUser.userType}"=="4"){
					alert("你无权修改备课树!");
					return;
				}
				var treeObj = $.fn.zTree.getZTreeObj("treeDemo3");
				var nodes = treeObj.getSelectedNodes();
				if(nodes!=null && nodes.length>0){ 
					$("#user_include").html("");
				 	$("#user_include").load("${pageContext.request.contextPath}/learnPlantUpdate.jsp?r="+Math.random(),nodes[0],function(){				 		 
				 		var dialog = $("#dialog-message").removeClass("hide").dialog({
							modal: true,
							title: "修改标签",
							width:1000,
							title_html: false,
							close:function(){
								$(this).dialog('destroy'); 
								$("#user_include").html(""); 
							},
							buttons: [  
								{
									text: "提交",
									"class" : "btn btn-primary btn-xs",
									click: function() {
										window.learnPlantUpdate.updatelearnPlant(this);
										 
									} 
								}
							]
						}); 
					}); 
				}else{
					alert("未选中节点！");
				}
		});
	};
	//查询所有的标签
	function loadlearnPlant(event,data){
		if(typeof(data)=="undefined"){
			data={};
		}
		var url ="${pageContext.request.contextPath}/edu/sys_LearnPlantAction.do?method=getAlllearnPlantBy4";
		var cp =$("#learnPlant_pagination").find(".active");
		data.start=1;
		data.limit=5;
		data.name=$("#serch_text").val();
		if(cp.length>0){
			var ccp=$(cp[0]).text();
			if(parseInt(ccp)){
				data.start=ccp;
			}
		}  
		$("#table_learnPlant").wgrid({
					table_title:"sys_learnPlant_table_title",
					tbodyId:"sys_learnPlant_table",
					pagination:"learnPlant_pagination",
					url:url,
					//checkbox:true,
					data:data,
					success:function(){
						changelearnPlant();
						//guanlianZuoye();
					}
		});
	};
	//关联作业
	function guanlianZuoye(e){
		var arr =$("div[name=sys_learnPlant_table_string2_tr_td]");   
		 for(var i=0;i<arr.length;i++){ 
			 var value =$(arr[i]).text(); 
			 if(value.length<=0){
				
				$(arr[i]).parent().parent().attr("id");
				var table_id=$(arr[i]).parent().parent().attr("id");
				var cdata =$("#"+table_id).data(table_id);
				 var a =$('<a href="javascript:;" table_id="'+table_id+'">关联作业</a>'); 
				 var text =$(arr[i]).text();
				$(arr[i]).html("");
				if(cdata.string1 && cdata.string1.length>0){ 
					$(arr[i]).append(a);
					$(arr[i]).append(text);
				}else{
					$(arr[i]).html(a);
				}
				a.click(function(e){
					  var table_id =$(this).attr("table_id");
				 
					 loadHomeWork(table_id);
					 e.stopPropagation(); 
				});
			 }else{
				  var text =$(arr[i]).text();
				 $(arr[i]).html("");
				 $(arr[i]).parent().parent().attr("id");
				 var table_id=$(arr[i]).parent().parent().attr("id"); 
				  var a =$('<a href="javascript:;" table_id="'+table_id+'">关联作业</a>'); 
				  $(arr[i]).append(a);
				  $(arr[i]).append(text); 
				  a.click(function(e){
					  var table_id =$(this).attr("table_id");
					  
					 loadHomeWork(table_id);
					 e.stopPropagation(); 
				  });
			 }
		 }
		// 
	};
	
	//显示作业弹窗
	function loadHomeWork(id){		
		$("#user_include").html("");
		$("#user_include").load("${pageContext.request.contextPath}/learnPlant_homeWork.jsp?r="+Math.random(),{table_id:id},function(){
			var dialog = $("#dialog-message").removeClass("hide").dialog({
				modal: true,
				title: "关联作业模块",
				width:1000,
				height:600,
				title_html: false,
				close:function(){
					$(this).dialog('destroy'); 
					$("#user_include").html("");
					//loadTree();
				},
				buttons: [  
					{
						text: "提交",
						"class" : "btn btn-primary btn-xs",
						click: function() {
							 window.learnPlant_homeWork.createHomeWorkLearnPlant(this);
						} 
					}
				]
			}); 
		}); 
	}; 
	//改变头像 和性别，还有类型
	function changelearnPlant(){ 
		$("tr[name=sys_learnPlant_table_tr]").click(function(){ 
			var cdata =$(this).data($(this).attr("id")); 
			var url ="${pageContext.request.contextPath}/edu/sys_LearnPlantAction.do?method=getLearnPlantChildren";		 
			$.ajax({
				   type: "POST",
				   url: url,
				   data: {lpid:cdata.lpid},
				   dataType:'json',
				   success: function(msg){   
					 if(msg && msg.length>0){
						 $("#learnPlant_cd1").html("");
						 for(var i=0;i<msg.length;i++){
							 	object = msg[i]; 
							 	var vt="";
							 	if(object.arrlable){
							 		var arr =object.arrlable;
							 		for(var j=0;j<arr.length;j++){
										if(j==arr.length-1){
											vt+='<button class="btn btn-xs btn-inverse" style="margin-top:4px;" name="lablebuton1" table_id="'+arr[j].table_id+'">'+arr[j].name+'</button>';								 
										}else{
											vt+='<button class="btn btn-xs btn-inverse" style="margin-top:4px;" name="lablebuton1" table_id="'+arr[j].table_id+'">'+arr[j].name+'</button>&nbsp;';
										}
									}
							 	}
								
								var buzhou5="";
								var buzhou3="";
								var buzhou6="";
								if(object.buzhou5){
									buzhou5=object.buzhou5;
								}
								if(object.buzhou3){
									buzhou5=object.buzhou3;
								}
								if(object.buzhou6){
									buzhou5=object.buzhou6;
								}
								
								var tr ='<tr name="buzhoutr">'+
						 			'<td>'+object.buzhou1+'</td>'+
						 			'<td>'+object.buzhou2+'</td>'+
						 			'<td>'+buzhou5+'</td>'+
						 			'<td>'+object.buzhou4+'</td>'+
						 			'<td>'+vt+'</td>'+
						 			'<td>'+buzhou3+'</td>'+
						 			'<td>'+buzhou6+'</td>'+ 
						 			'</tr>';
								if($("#learnPlant_cd1").length>0){
									 var  tr1 =$(tr);
									 tr1.data("pl",object); 
									 $("#learnPlant_cd1").append(tr1); 
								}  
						 } 
					 }
				   }
			}); 
		});
	}
	//加载步骤
	function loadChildrenBUzhou(){
		var url ="${pageContext.request.contextPath}/edu/sys_LearnPlantAction.do?method=getLearnPlantChildren";		 
		$.ajax({
			   type: "POST",
			   url: url,
			   data: {lpid:"${param.lpid}"},
			   dataType:'json',
			   success: function(msg){  
				 if(msg && msg.length>0){
					 for(var i=0;i<msg.length;i++){
						 	object = msg[i]; 
						 	var vt="";
						 	if(object.arrlable){
						 		var arr =object.arrlable;
						 		for(var j=0;j<arr.length;j++){
									if(j==arr.length-1){
										vt+='<button class="btn btn-xs btn-inverse" style="margin-top:4px;" name="lablebuton1" table_id="'+arr[j].table_id+'">'+arr[j].name+'<i class="icon-remove"></i></button>';								 
									}else{
										vt+='<button class="btn btn-xs btn-inverse" style="margin-top:4px;" name="lablebuton1" table_id="'+arr[j].table_id+'">'+arr[j].name+'<i class="icon-remove"></i></button>&nbsp;';
									}
								}
						 	}
							var tr ='<tr name="buzhoutr">'+
					 			'<td>'+object.buzhou1+'</td>'+
					 			'<td>'+object.buzhou2+'</td>'+
					 			'<td>'+object.buzhou5+'</td>'+
					 			'<td>'+object.buzhou4+'</td>'+
					 			'<td>'+vt+'</td>'+
					 			'<td>'+object.buzhou3+'</td>'+
					 			'<td>'+object.buzhou6+'</td>'+
					 			'<td><button class="btn btn-xs" name="btndelete">删除</button></td>'+
					 			'</tr>';
							if($("#childrenLv5").length>0){
								 var  tr1 =$(tr);
								 tr1.data("pl",object); 
								 $("#childrenLv5").append(tr1); 
							} 
						 
					 }
					$("#learnPlantAdd_ClassWindow").html("");
					$("button[name=lablebuton1]").unbind().click(function(){
						var data =$(this).parent().parent().data("pl");
						if(data && data.arrlable && data.arrlable.length>0){
							var arr = new Array();
							for(var i=0;i<data.arrlable.length;i++){
								if(data.arrlable[i].table_id != $(this).attr("table_id")){
									arr.push(data.arrlable[i]);
								}
							}
							data.arrlable =arr;
							$(this).parent().parent().data("pl",data);
						} 
						$(this).remove();
					});
					$("button[name=btndelete]").click(function(){
						$(this).parent().parent().remove();
					});
				 }
			   }
		}); 
	};
	//搜索的回车事件
	$('#serch_text').keydown(function(e){ 
		if(e.keyCode==13){
			loadlearnPlant(null,{name:$('#serch_text').val()});
		}
	});
	$(function(){
		addClass();
		loadTree();
		loadlearnPlant(null,{});
	});
	window.learnPlant={};
	window.learnPlant.loadTree=loadTree;
	window.learnPlant.loadlearnPlant=loadlearnPlant;
})();
</script>